package cn.sinata.wanglin.mine.activity;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sinata.wanglin.MainActivity;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.db.dao.UserDao;
import cn.sinata.wanglin.model.LoginInfo;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class LoginStyleActivity extends BaseActivity {
    @BindView(R.id.layout_fromaccout)
    RelativeLayout fromaccout;
    @OnClick({R.id.layout_fromqq, R.id.layout_fromweixin ,R.id.layout_fromaccout})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_fromqq:
                showDialog("拉取中...");
                UMShareAPI.get(this).getPlatformInfo(LoginStyleActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> data) {
                        Utils.systemErr(data);
                        dismissDialog();
                        String openid =data.containsKey("openid")?data.get("openid"):"";
                        String name = data.containsKey("name")?data.get("name"):"";
                        String gender =data.containsKey("gender")?data.get("gender"):"";
                        String iconurl =data.containsKey("iconurl")?data.get("iconurl"):"";
                        Utils.systemErr( "qq_openid"+openid);
                        autoLogin(openid,"A",iconurl,name,gender);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        dismissDialog();
                        showToast("拉取失败，请采用其他方式登录！");

                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {
                        dismissDialog();
                    }
                });
                break;
            case R.id.layout_fromweixin:
                showDialog("拉取中...");
                UMShareAPI.get(this).getPlatformInfo(LoginStyleActivity.this, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
                    @Override
                    public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> data) {
                        dismissDialog();
                        String openid =data.containsKey("openid")?data.get("openid"):"";
                        String name = data.containsKey("name")?data.get("name"):"";
                        String gender =data.containsKey("gender")?data.get("gender"):"";
                        String iconurl =data.containsKey("iconurl")?data.get("iconurl"):"";
                        autoLogin(openid,"B",iconurl,name,gender);
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                        dismissDialog();
                        showToast("拉取失败，请采用其他方式登录！");
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media, int i) {
                        dismissDialog();
                    }
                });
                break;
            case R.id.layout_fromaccout:
                ActivityUtil.create(mContext).go(AccoutLoginActivity.class)
                        .startForResult(2);
                break;

        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_style);
        ButterKnife.bind(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            switch (requestCode){
                case 2:
                    finish();
                    break;
                case 4:
                    ActivityUtil.create(mContext).go(MainActivity.class)
                            .start();
                    finish();
                    break;
            }
        }

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        UMShareAPI.get(this).release();
    }
    public void autoLogin(final String openID, final String outhType, final String headUrl, final String nickName, final String sex){

        showDialog("登录中...");
        HttpManager.authLogin(openID,outhType,headUrl,nickName,sex).subscribe(new ResultDataSubscriber<LoginInfo>(this) {
            @Override
            public void onSuccess(String msg, LoginInfo loginInfo) {
                showToast(msg);
                SharedPreferencesUtils.save(Constant.SP_LOGIN_OPENID,openID);
                SharedPreferencesUtils.save(Constant.SP_LOGIN_OUTHTYPE,outhType);
                SharedPreferencesUtils.save(Constant.SP_LOGIN_HEADURL,headUrl);
                SharedPreferencesUtils.save(Constant.SP_LOGIN_NICKNAME,nickName);
                SharedPreferencesUtils.save(Constant.SP_LOGIN_SEX,sex);
                SharedPreferencesUtils.save(Constant.SP_LOGIN_TYPE,outhType);
                SharedPreferencesUtils.save(Constant.SP_USERID,loginInfo.getId());
                SharedPreferencesUtils.save(Constant.SP_HOUSING_ID,loginInfo.getHousingID());
                SharedPreferencesUtils.save(Constant.SP_HOUSING_NAME,loginInfo.getHousingName());
                SharedPreferencesUtils.save(Constant.SP_WANGLINID,loginInfo.getAccount());
                SharedPreferencesUtils.save(Constant.SP_IM_ACCOUNT,loginInfo.getAccount());
                SharedPreferencesUtils.save(Constant.SP_IM_PWD,loginInfo.getImPassword());
                SharedPreferencesUtils.save(Constant.SP_HEADURL,loginInfo.getHeadUrl());
                if(TextUtils.isEmpty(loginInfo.getSex())){
                    SharedPreferencesUtils.save(Constant.SP_SEX,"1");
                }else{
                    SharedPreferencesUtils.save(Constant.SP_SEX,loginInfo.getSex());
                }
                if(TextUtils.isEmpty(loginInfo.getIsforeign())||"0".equals(loginInfo.getIsforeign())){
                    SharedPreferencesUtils.save(Constant.SP_FIND_ME,true);
                }else{
                    SharedPreferencesUtils.save(Constant.SP_FIND_ME,false);
                }
                if(TextUtils.isEmpty(loginInfo.getValid())||"0".equals(loginInfo.getValid())){
                    SharedPreferencesUtils.save(Constant.SP_NEED_AUTH,true);
                }else{
                    SharedPreferencesUtils.save(Constant.SP_NEED_AUTH,false);
                }
                String nick = loginInfo.getNickName();
                if(TextUtils.isEmpty(nick)){
                    nick = "网龄用户";
                }
                SharedPreferencesUtils.save(Constant.SP_NICKNAME,nick);
                UserDao.getInstance(mContext).saveUser(loginInfo.getAccount(),nick,loginInfo.getHeadUrl());
                if(!TextUtils.isEmpty(loginInfo.getPhone())){
                    SharedPreferencesUtils.save(Constant.SP_PHONE,loginInfo.getPhone());
                    ActivityUtil.create(mContext).go(MainActivity.class)
                            .start();
                    finish();
                }else{
                    ActivityUtil.create(mContext).go(BindPhoneForAuthActivity.class).put("fromWhere","login")
                            .startForResult(4);
                }

            }
        });
    }
}
